<?php
namespace app\common\controller;
use think\Controller;
use alyun\AliyunOss;

class Base extends Controller
{
    
    protected function _initialize()
    {
    
    }
    
    /**
     * 上传图片到阿里云
     */

//media/
//
//pic/
//
//upload/
//
//video/
    protected function uploadToAlyun($file,$dir=null)
    {
        $dir = $dir?$dir.'/':'upload/';//存放目录,默认upload
        $url = AliyunOss::uploadFile($dir,$file);
        return $url;
    }
    
    public function ueditor(){
        date_default_timezone_set("PRC");
        error_reporting(E_ERROR);
        header("Content-Type: text/html; charset=utf-8");
        $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents("./ueditor/php/config.json")), true);
        $action = $_GET['action'];
        switch ($action) {
            case 'config':
                $result =  json_encode($CONFIG);
                break;
            
            /* 上传图片 */
            case 'uploadimage':
                /* 上传涂鸦 */
            case 'uploadscrawl':
                /* 上传视频 */
            case 'uploadvideo':
                /* 上传文件 */
            case 'uploadfile':
                $result = include("./ueditor/php/action_upload.php");
                break;
            
            /* 列出图片 */
            case 'listimage':
                $result = include("./ueditor/php/action_list.php");
                break;
            /* 列出文件 */
            case 'listfile':
                $result = include("./ueditor/php/action_list.php");
                break;
            
            /* 抓取远程文件 */
            case 'catchimage':
                $result = include("./ueditor/php/action_crawler.php");
                break;
            
            default:
                $result = json_encode(array(
                    'state'=> '请求地址出错'
                ));
                break;
        }
        
        /* 输出结果 */
        if (isset($_GET["callback"])) {
            if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
                echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
            } else {
                echo json_encode(array(
                    'state'=> 'callback参数不合法'
                ));
            }
        } else {
            $row = json_decode($result,true);
//            var_dump($row);die;
            //阿里云对象存储
//            'aliyun_oss'=>[
//                'AccessKeyID' => 'LTAInsYXH5gdFYzS',
//                'AccessKeySecret' => '0kp2xxxwmRhhYwunlAMr1wHaix1bXi',
//                'endpoint' => 'oss-cn-shanghai.aliyuncs.com',
//                'bucket' => 'yueyang-live2'
//            ],
            $aliyunConfig = config('thirdaccount.aliyun');
            $ossConfig = config('thirdaccount.aliyun_oss');
            if($row['state'] == 'SUCCESS'){
                vendor('aliyun.autoload');
                $accessKeyId = $aliyunConfig['accessKeyId'];//去阿里云后台获取秘钥
                $accessKeySecret = $aliyunConfig['accessKeySecret'];//去阿里云后台获取秘钥
                $endpoint = $ossConfig['endpoint'];//你的阿里云OSS地址
                $ossClient = new \OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint);
                $bucket= $ossConfig['bucket'];//oss中的文件上传空间
                $object = 'upload/image/'.date('Ymd',time()). '/'.$row['title'];//想要保存文件的名称
                $file = './'.$row['url'];
//                $file = '/ueditor/php/upload/image/' . $row['title'];//文件路径，必须是本地的。
                try{
                    $ossClient->uploadFile($bucket,$object,$file);
                    //上传后不删除本地文件,请删除或注释这一行
                    unlink($file);
                } catch(OssException $e) {
                    return;
                }
            }
            echo $result;
        }
    }
    
}